package com.frank.subject.domain.handler.subject;

import com.frank.common.enums.IsDeletedFlagEnum;
import com.frank.subject.common.enums.SubjectInfoTypeEnum;
import com.frank.subject.domain.convert.SubjectMultipleBoConverter;
import com.frank.subject.domain.entity.SubjectAnswerBo;
import com.frank.subject.domain.entity.SubjectInfoBo;
import com.frank.subject.domain.entity.SubjectOptionBo;
import com.frank.subject.infra.basic.entity.SubjectMultiple;
import com.frank.subject.infra.basic.service.SubjectMultipleService;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/1/25 2:17 PM
 * @description 多选题类型策略类
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }


    @Override
    public void add(SubjectInfoBo subjectInfoBO) {
        List<SubjectMultiple> list = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(subjectAnswerBO -> {
            Preconditions.checkNotNull(subjectAnswerBO.getOptionType(), "Option type is be empty!");
            Preconditions.checkNotNull(subjectAnswerBO.getOptionContent(), "Option content is be empty!");
            Preconditions.checkNotNull(subjectAnswerBO.getIsCorrect(), "Option correct is be empty!");
            SubjectMultiple subjectMultiple = SubjectMultipleBoConverter.INSTANCE.convertBoToEntity(subjectAnswerBO);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            list.add(subjectMultiple);
        });
        subjectMultipleService.saveBatch(list);
    }

    @Override
    public SubjectOptionBo query(Long subjectId) {
        List<SubjectMultiple> options = subjectMultipleService.queryBySubjectId(subjectId);
        List<SubjectAnswerBo> subjectAnswerBos = SubjectMultipleBoConverter.INSTANCE.convertEntityListToBoList(options);
        SubjectOptionBo subjectOptionBo = new SubjectOptionBo();
        subjectOptionBo.setOptionList(subjectAnswerBos);
        return subjectOptionBo;
    }
}
